<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Title</title>
<head>
</head>
<style>
    .layui-form-selectup dl {
        top: 42px;
        bottom: unset;
    }
</style>
<body>
</body>
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">待业人员</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>待业人员</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="industry" name="industry" lay-filter="industry">
                <option value="" style="margin-top: 50px;">行业名-</option>
            </select>
            <select id="position" name="position" lay-filter="position">
                <option value="">职位名-</option>
            </select>
            <select id="employedStatus" name="employedStatus" lay-filter="employedStatus">
                <option value="">就业状态-</option>
                <option value="0">待业中</option>
                <option value="1">已就业</option>
            </select>
            <!--            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="职位名"/>&emsp;-->
            <button id="user-btn-search" class="layui-btn icon-btn"><i
                    class="layui-icon">&#xe615;</i>搜索
            </button>
<!--            <button id="user-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>-->
            <button id="user-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="recruitment-table" lay-filter="recruitment-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<!--<script type="text/html" id="recruitment-table-bar">-->
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
<!--    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>-->
<!--</script>-->

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-state">
    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>


    layui.use(['form', 'table', 'util', 'config', 'admin', 'formSelects'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var formSelects = layui.formSelects;


        // 手动联动行业&&职位
        let pid = 0;
        let pidd = JSON.stringify(pid);
        // 默认查询行业
        $.ajax({
            url: config.base_server + 'api-recruitment/industry/findIndustry',
            // data:JSON.stringify({pid:pid}),
            data: JSON.stringify(pid),
            type: 'post',
            contentType: 'application/json',
            beforeSend: function (xhr) {
                let token = config.getToken()
                if (token) {
                    // xhr.setRequestHeader('Authorization', 'Basic ' + token.access_token);
                    xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                }
            },
            dataType: 'JSON',
            async: false,
            success: function (res) {
                for (let i = 0; i < res.data.length; i++) {
                    var industryName = res.data[i].industryName;
                    var value = res.data[i].id;
                    // 使用循环遍历，给下拉列表赋值
                    $('#industry').append(new Option(industryName, value));
                }
            }
        })
        form.render();
        // 默认查询职位
        layui.use(['layer', 'jquery', 'form'], function () {
            var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form;
            form.on('select(industry)', function (data) {
                var pid = $("#industry").val();

                $.ajax({
                    url: config.base_server + 'api-recruitment/industry/findPosition',
                    // data:JSON.stringify({pid:pid}),
                    data: JSON.stringify(pid),
                    type: 'post',
                    contentType: 'application/json',
                    beforeSend: function (xhr) {
                        let token = config.getToken()
                        if (token) {
                            xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                        }
                    },
                    dataType: 'JSON',
                    async: false,
                    success: function (res) {
                        for (let i = 0; i < res.data.length; i++) {
                            var industryName = res.data[i].industryName;
                            var value = res.data[i].id;
                            // 使用循环遍历，给下拉列表赋值
                            $('#position').append(new Option(industryName, value));
                        }
                    }
                })
                form.render();
            });
        });

        // 添加按钮绑定权限
        if (!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-add').hide();
        }

        // 导出按钮绑定权限
        if (!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-export').hide();
        }
        // 搜索按钮点击事件 // 模糊查询
        $('#user-btn-search').on('click', function () {
            let data = {
                industry: $('#industry').val(),
                position: $('#position').val(),
                employedStatus: $('#employedStatus').val(),
                page: 0,
                limit: 0
            };
            $.ajax({
                url: config.base_server + 'api-recruitment/industry/selectUnemployed/' + data.industry + '/' + data.position + '/' + data.employedStatus + '/' + data.page + '/' + data.limit,
                data: null,//JSON.stringify(data)
                type: 'post',
                contentType: 'application/json',
                beforeSend: function (xhr) {
                    let token = config.getToken()
                    if (token) {
                        xhr.setRequestHeader('Authorization', 'Bearer ' + token.access_token);
                    }
                },
                dataType: 'JSON',
                async: false,
                success: function (res) {
                    console.log(res.data, 'huge');
                    table.render({
                        elem: '#recruitment-table',
                        data: res.data,
                        page: true,
                        cols: [[
                            {type: 'numbers'},
                            {field: 'id', sort: true, title: '用户编号'},
                            {field: 'username', sort: true, title: '账号'},
                            {field: 'nickname', sort: true, title: '用户名'},
                            {field: 'phone', sort: true, title: '手机号'},
                            {
                                sort: true, title: '性别', templet: function (d) {
                                    return d.sex == 0 ? "男" : "女"
                                }
                            },
                            {
                                sort: true, title: '就业状态', templet: function (data) {
                                    return data.employed == 0 ? "待业中" : "已就业"
                                }
                            },
                            {
                                sort: true, templet: function (d) {
                                    return util.toDateString(d.createTime);
                                }, title: '注册时间'
                            },
                            // {field: 'enabled', sort: true, templet: '#user-tpl-state', title: '状态',width: 150},
                            // {align: 'center', toolbar: '#recruitment-table-bar', title: '操作', width: 150}
                        ]]
                    })
                }
            })
            form.render();

        });
        // 渲染表格
        table.render({
            elem: '#recruitment-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-recruitment/recruitment/findUnemployed',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            // headers: {
            //     Authorization:'Bearer '+ config.getToken().access_token
            // },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '用户编号'},
                {field: 'username', sort: true, title: '账号'},
                {field: 'nickname', sort: true, title: '用户名'},
                {field: 'phone', sort: true, title: '手机号'},
                {
                    sort: true, title: '性别', templet: function (d) {
                        return d.sex == 0 ? "男" : "女"
                    }
                },
                {field: 'employedStatus', sort: true, title: '就业状态'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '注册时间'
                },
                // {field: 'enabled', sort: true, templet: '#user-tpl-state', title: '状态',width: 150},
                // {align: 'center', toolbar: '#recruitment-table-bar', title: '操作', width: 150}
            ]]
        });

        // // 添加按钮点击事件
        // $('#user-btn-add').click(function () {
        //     showEditModel();
        // });
        //
        // // 工具条点击事件
        // table.on('tool(recruitment-table)', function (obj) {
        //     var data = obj.data;
        //     var layEvent = obj.event;
        //
        //     if (layEvent === 'edit') { // 修改
        //         showEditModel(data);
        //     } else if (layEvent === 'reset') { // 重置密码
        //         layer.confirm('确定重置此用户的密码吗？', function (i) {
        //             layer.close(i);
        //             layer.load(2);
        //             admin.req('api-user/users/' + obj.data.id + '/resetPassword', {}, function (data) {
        //                 layer.closeAll('loading');
        //                 if (data.code == 0) {
        //                     layer.msg(data.msg, {icon: 1, time: 500});
        //                 } else {
        //                     layer.msg(data.msg, {icon: 2, time: 500});
        //                 }
        //             }, 'POST');
        //         });
        //     }
        // });
        //
        // //显示表单弹窗
        // var showEditModel = function (data) {
        //     //修改sex值
        //     if (data) {
        //         data.sex = data.sex + '';
        //     }
        //     admin.putTempData('t_user', data);
        //     var title = data ? '修改用户' : '添加用户';
        //     admin.popupCenter({
        //         title: title,
        //         path: 'pages/system/user_form.html',
        //         finish: function () {
        //             table.reload('recruitment-table', {});
        //         }
        //     });
        // };


        // 无条件直接导出
        $('#user-btn-export').click(function () {
            var url = config.base_server + 'api-recruitment/industry/exportUnemployed?access_token=' + config.getToken().access_token;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', url, true);
            xhr.responseType = "blob";
            xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            xhr.onload = function () {
                if (this.status == 200) {
                    var blob = this.response;
                    var objecturl = URL.createObjectURL(blob);
                    window.location.href = objecturl;
                }
            }
            xhr.send();
        });

        // 修改user状态
        form.on('switch(user-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-user/users/updateEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });
</script>
</html>